package com.fs.groupSeven.service.impl;

import com.fs.groupSeven.Dao.SubjectDao;

import com.fs.groupSeven.pojo.Class;
import com.fs.groupSeven.pojo.College;
import com.fs.groupSeven.pojo.Teacher;
import com.fs.groupSeven.service.SubjectService;
import com.fs.groupSeven.pojo.Subject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;
/**
 * @description:
 * @author： mjx
 * @createDate: 2020/9/28
 * @version: 1.0
 */
//IOC容器帮我们去new这个实现对象
//    IOC容器帮我们去new这个实现对象
@Service
public class SubjectServiceImpl implements SubjectService {
    private SubjectDao subjectDao;

    @Autowired//自动装配 依赖注入
    public SubjectServiceImpl(SubjectDao subjectDao) {
        this.subjectDao = subjectDao;
    }

    @Override
    public List<Teacher> getTeacherList(Integer collegeId) {
        List<Teacher> teachers = subjectDao.queryTeacherList(collegeId);
        return teachers;
    }

    @Override
    public Subject getOneBySubjectId(Integer subjectId) {
        return subjectDao.queryOneBySubjectId(subjectId);
    }

    @Override
    public PageInfo<Subject> getSubjectsByPage(Integer currentPage, Integer pageSize,Integer collegeId) {
        //准备拦截mybatis的后续查询
        List<Subject> subjectList;
        PageHelper.startPage(currentPage,pageSize);
        subjectList = subjectDao.querySubjectList(collegeId);
        //pagehelper内部帮我们计算总共有多少页
        PageInfo<Subject> pageInfo = new PageInfo<>(subjectList);
        return pageInfo;
    }

    @Override
    public List<Class> getClassList(Integer subjectId) {
        List<Class> classes = subjectDao.queryClassList(subjectId);
        return classes;
    }

    @Override
    public PageInfo<Subject> getListByPageAndSubjectName(Integer currentPage, Integer pageSize,Integer collegeId,String subjectName) {
        //准备拦截mybatis的后续查询
        List<Subject> subjectList;
        PageHelper.startPage(currentPage,pageSize);
        subjectList = subjectDao.queryByColumn(collegeId,subjectName);
        //pagehelper内部帮我们计算总共有多少页
        PageInfo<Subject> pageInfo = new PageInfo<>(subjectList);
        return pageInfo;
    }

    @Override
    public boolean addOneSubject(Subject subject) {
        int result = subjectDao.addOneBySubject(subject);
        if (subject.getTeacher() != null) {
            int subjectId = subject.getSubjectId();
            int teacherId = subject.getTeacher().getTeacherId();
            subjectDao.insertTeacherSubject(teacherId, subjectId);
        }
        return result>0;
    }

    @Override
    public boolean modifyOneSubject(Subject subject) {
        if (subject.getTeacher() != null) {
        int subjectId = subject.getSubjectId();
        int teacherId = subject.getTeacher().getTeacherId();
        subjectDao.updateTeacherSubject(teacherId, subjectId);
    }
       int result = subjectDao.updateBySubject(subject);
       return result >0;
    }

    @Override
    public boolean removeOneSubject(Subject subject) {
        int result = subjectDao.deleteBySubject(subject);
        return result>0;
    }

    @Override
    public List<College> getCollegeList() {
        return  subjectDao.queryCollegeList();

    }

    @Override
    public List<Subject> getSubjectList() {
        List<Subject> subjects = subjectDao.querySubjectList(1);
//        System.out.println("---"+subjects);
        return subjects;
    }
}
